//
//  PopoverAnimator.swift
//  JIMWeiBo
//
//  Created by 陈恒均 on 2018/2/10.
//  Copyright © 2018年 陈恒均. All rights reserved.
//

import UIKit

class PopoverAnimator: NSObject {
    // MARK:- 对外提供的属性
    var isPresented : Bool = false
    var presentedFrame : CGRect = CGRect.zero
    
    var callBack : ((_ presented : Bool)->())?
    
    
    init(callBack : @escaping (_ presented : Bool)->()) {
        self.callBack = callBack
    }
    
    
    
}
// MARK:- 自定义转场代理的方法
extension PopoverAnimator:UIViewControllerTransitioningDelegate
{
    //目的:改变弹出的View尺寸
    func presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController? {
        
        let presentation = CHJPresentationController(presentedViewController: presented, presenting: presenting)
        presentation.presentedFrame = presentedFrame
        return presentation
    }
    //目的:自定义淡出的动画
    func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        
        isPresented = true
        self.callBack!(isPresented)
        return self
    }
    func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresented = false
        self.callBack!(isPresented)
        return self
    }
    
    
}
// MARK:- 弹出和消失动画的代理方法
extension PopoverAnimator:UIViewControllerAnimatedTransitioning
{
    //动画执行时间
    func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
        return 0.5
    }
    
    /// 获取`转场的上下文`:可以通过转场上下文获取弹出的View和消失的View
    // UITransitionContextFromViewKey : 获取消失的View
    // UITransitionContextToViewKey : 获取弹出的View
    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
        isPresented ? animationForPresentedView(transitionContext: transitionContext) : animationForDismissView(transitionContext: transitionContext)
    }
    //自定义转场动画
    func animationForPresentedView(transitionContext: UIViewControllerContextTransitioning) {
        let presentedView = transitionContext.view(forKey: .to)
        
        transitionContext.containerView.addSubview(presentedView!)
        
        presentedView?.transform = CGAffineTransform(scaleX: 1.0, y: 0.0)
        
        presentedView?.layer.anchorPoint = CGPoint(x: 0.5, y: 0)
        
        UIView.animate(withDuration: self.transitionDuration(using: transitionContext), animations: {
            presentedView?.transform = CGAffineTransform.identity
        }) { (_) in
            
            //告诉转场上下文你已经完成动画
            transitionContext.completeTransition(true)
        }
        
        
    }
    //自定义消失动画
    private func animationForDismissView(transitionContext: UIViewControllerContextTransitioning)
    {
        let dismissView = transitionContext.view(forKey: .from)
        
        UIView.animate(withDuration: self.transitionDuration(using: transitionContext), animations: {
            dismissView?.transform = CGAffineTransform(scaleX: 1.0, y: 0.1)
        }) { (_) in
            dismissView?.removeFromSuperview()
            transitionContext.completeTransition(true)
        }
        
    }
    
    
}

